/*
 * @Descripttion: service
 * @version: 1.0.0
 * @Author: ErCHen
 * @Date: 2022-03-15 20:06:09
 * @LastEditTime: 2022-03-15 20:07:12
 */
import httpClient from '../utils/client'
import {
  SolveLog, Collect, Breakpoint, MessageSend,
  CommentSend, ReplySend, HistoryAnswer, RestPass, AnswerReq
} from '@/utils/ds'

export function hello (url: string) {
  return httpClient.get(url)
}

function encodeUrl (data: Map<string, string | number>) {
  const arr: string[] = []
  data.forEach((value, key, map) => {
    arr.push(encodeURI(key) + '=' + encodeURI(value.toString()))
  })
  if (arr.length === 0) {
    return ''
  }
  return '?' + arr.join('&')
}

export function upload408Question (data: any) {
  return httpClient.post('/question/408', data)
}

export function getQesByYearNumber (data: Map<string, any>) {
  const query = encodeUrl(data)
  const url = '/question' + query
  return httpClient.get(url)
}

export function existQus (type: string, year: string, number: number) {
  const url = `/question/exist?type=${type}&year=${year}&number=${number}`
  return httpClient.get(url)
}

export function register (data: any, params: Map<string, string | number>) {
  const query = encodeUrl(params)
  const url = '/register' + query
  return httpClient.post(url, data)
}

export function login (data: any) {
  const url = '/login'
  return httpClient.post(url, data)
}

export function getUserRanking () {
  const url = '/user/ranks'
  return httpClient.get(url)
}

export function getCategoryData (data: string) {
  const url = '/question/category'
  return httpClient.post(url, data)
}

export function getSubjectCategories (subject: string) {
  const url = `/subject/category?subject=${subject}`
  return httpClient.get(url)
}

export function getSubjectCategoryId (subject: string, cate: string, start: number, count: number) {
  const url = `/subject/category?subject=${subject}&cate=${cate}&start=${start}&count=${count}`
  return httpClient.post(url, null)
}

export function getTargetQus (data: number[]) {
  const url = '/question'
  return httpClient.post(url, data)
}

export function addRecordLog (data:SolveLog[]) {
  const url = '/question/solve/log'
  return httpClient.post(url, data)
}

export function checkRegisterField (params: Map<string, string>) {
  const url = '/register/check'
  const d = encodeUrl(params)
  return httpClient.get(url + d)
}

export function getValidCode (email: string) {
  const url = `/user/reset/code?email=${email}`
  return httpClient.get(url)
}

export function resetPasswd (data: RestPass) {
  const url = '/user/reset/passwd'
  return httpClient.post(url, data)
}

export function resetName (name: string, token: string) {
  const url = `/user/reset/name?name=${name}&token=${token}`
  return httpClient.get(url)
}

export function getUserStatistic (token: string) {
  const url = '/user/statistic?op=' + token
  return httpClient.post(url, null)
}

export function addUserCollect (data: Collect[]) {
  const url = '/user/collect'
  return httpClient.post(url, data)
}

export function getUserCollect () {
  const url = '/user/collect'
  return httpClient.get(url)
}

export function getCollectBySub (cate: number) {
  const url = `/user/collect/sub?cate=${cate}`
  return httpClient.get(url)
}

export function deleteCollect (id: number) {
  const url = `/user/collect/delete?id=${id}`
  return httpClient.get(url)
}

export function getQusDescription (ids:number[]) {
  const url = '/question/collect'
  return httpClient.post(url, ids)
}

export function getUserLog (token: string) {
  const url = '/question/solve/log?token=' + token
  return httpClient.get(url)
}

export function getLogQusDetail (token: number) {
  const url = '/question/solve/detail?token=' + token
  return httpClient.get(url)
}

export function getSubjectCate () {
  const url = '/question/query/cate'
  return httpClient.get(url)
}

export function getQusByYearNumberV2 (year: string, number: string, type: string) {
  const url = `/question/query?year=${year}&number=${number}&type=${type}`
  return httpClient.get(url)
}

// export function getSubjectAllID (subject: string) {
//   const url = '/question/query/subject?subject=' + subject
//   return httpClient.get(url)
// }

export function getQusByYear (year: string) {
  const url = `/question/query/year?year=${year}`
  return httpClient.get(url)
}

export function getAllSubjectAllID (subjects: string[]) {
  const url = '/question/query/subjects'
  return httpClient.post(url, subjects)
}

export function getQusBySubjectTag (tags: string[]) {
  const url = '/question/query/tag'
  return httpClient.post(url, tags)
}

export function addBreakpoint (data: Breakpoint) {
  const url = '/user/breakpoint'
  return httpClient.post(url, data)
}

export function getBreakpoint () {
  const url = '/user/breakpoint'
  return httpClient.get(url)
}

export function getMsgNum () {
  const url = '/msg/num'
  return httpClient.get(url)
}

export function addMsg (data: MessageSend) {
  const url = '/msg'
  return httpClient.post(url, data)
}

export function getMsg (offset: number, count: number) {
  const url = `/msg?offset=${offset}&count=${count}`
  return httpClient.get(url)
}

export function getQusForPDF (ids:number[]) {
  const url = '/ep32'
  return httpClient.post(url, ids)
}

export function getMsgNumUnread () {
  const url = '/msg/num/unread'
  return httpClient.get(url)
}

export function setMsgRead (id:number) {
  const url = `/msg/read?id=${id}`
  return httpClient.post(url, null)
}

export function setMsgDelete (id:number) {
  const url = `/msg/delete?id=${id}`
  return httpClient.post(url, null)
}

// comment 相关
export function getCommentUnread () {
  const url = '/comment/num'
  return httpClient.get(url)
}

export function addComment (data: CommentSend) {
  const url = '/comment'
  return httpClient.post(url, data)
}

export function deleteComment (id: number) {
  const url = `/comment/delete?id=${id}`
  return httpClient.post(url, null)
}

export function addReply (data: ReplySend) {
  const url = '/reply'
  return httpClient.post(url, data)
}

export function getComment (pid: number, offset: number, count: number) {
  const url = `/comment?pid=${pid}&offset=${offset}&count=${count}`
  return httpClient.get(url)
}

export function getConversation (cid: number, offset: number, count: number) {
  const url = `/conversation?cid=${cid}&offset=${offset}&count=${count}`
  return httpClient.get(url)
}

export function commentLike (id: number, op: number) {
  const url = `/comment/like?id=${id}&op=${op}`
  return httpClient.get(url)
}

export function commentDislike (id: number, op: number) {
  const url = `/comment/dislike?id=${id}&op=${op}`
  return httpClient.get(url)
}

export function getUserReviewList () {
  const url = '/user/review'
  return httpClient.get(url)
}

export function getUserHistoryAnswer (subject: string) {
  const url = `/user/history/answer?subject=${subject}`
  return httpClient.get(url)
}

export function setUserHistoryAnswer (subject: string, data: HistoryAnswer[]) {
  const url = `/user/history/answer?subject=${subject}`
  return httpClient.post(url, data)
}

export function userAddAnswer (data: AnswerReq) {
  const url = '/answer/add'
  return httpClient.post(url, data)
}

export function useDelAnswer (id: number) {
  const url = `/answer/del?id=${id}`
  return httpClient.get(url)
}

export function getUserAnswer (pid: number) {
  const url = `/answer/query?pid=${pid}`
  return httpClient.get(url)
}

export function getQusById (pid: number) {
  const url = `/question/query/id?pid=${pid}`
  return httpClient.get(url)
}
